<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件流下载</title>
</head>

<body>
    <button id="btn">download</button>
    <script>
        const btn = document.getElementById('btn');
        btn.addEventListener('click', () => {
            fetch('http://localhost:3001/download', {
                method: 'POST',
                headers: {
                    "content-type": 'application/json'
                },
                body: JSON.stringify({
                    filename: 'about.png'
                })
            }).then((res) => res.arrayBuffer()).then((buffer) => {
                //1.创建blob对象
                const blob = new Blob([buffer], { type: 'image/png' });
                //2.创建a标签
                const a = document.createElement('a');
                //3.设置a标签的href属性
                a.href = URL.createObjectURL(blob);
                //4.设置a标签的download属性
                a.download = 'about.png';
                //5.触发a标签的点击事件
                a.click();
                //6.释放URL对象
                URL.revokeObjectURL(a.href);
                //7.删除a标签
                document.body.removeChild(a);
            })
        })
    </script>
</body>

</html>